package com.example.demo.controller;

import com.example.demo.req.DocQueryReq;
import com.example.demo.req.DocSaveReq;
import com.example.demo.resp.CommonResp;
import com.example.demo.resp.DocQueryResp;
import com.example.demo.resp.PageResp;
import com.example.demo.service.DocService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/doc")
public class DocController {

    @Resource
    private DocService docService;
    @GetMapping("/list")
    public CommonResp list(@Valid DocQueryReq req){
        //把查询结果放到列表中
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        //创建一个CommonResp类型对象resp
        PageResp<DocQueryResp> list =docService.list(req);
        //将resp对象中的内容设置为查询结果列表list
        resp.setContent(list);
        //返回resp对象
        return resp;
    }
    @GetMapping("/all")
    public CommonResp all(){
        //把查询结果放到列表中
        CommonResp<List<DocQueryResp>> resp =new CommonResp<>();
        //创建一个CommonResp类型对象resp
        List<DocQueryResp> list =docService.all();
        //将resp对象中的内容设置为查询结果列表list
        resp.setContent(list);
        //返回resp对象
        return resp;
    }
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody DocSaveReq req){
        CommonResp resp = new CommonResp<>();
        docService.save(req);
        return resp;
    }
    @DeleteMapping("/delete/{idsStr}")
    public CommonResp delete(@PathVariable String idsStr){
        CommonResp resp = new CommonResp<>();
        List<String> list = Arrays.asList(idsStr.split(","));
        docService.delete(list);
        return resp;
    }

}